package com.huizhonglm.diap.ui.user;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.bruce.common.utils.DateUtil;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.user.OrderInfo;
import com.huizhonglm.diap.core.image.Thumb;
import com.huizhonglm.diap.widget.RoundRectImageView;

import java.util.List;

/**
 * 附近商品列表适配器
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-07 13:42
 */
public class OrderListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<OrderInfo> data;

    private OnPaymentListener paymentListener;

    public OrderListAdapter(LayoutInflater inflater, List<OrderInfo> data) {
        this.inflater = inflater;
        this.data = data;
    }

    public void setData(List<OrderInfo> data){
        this.data = data;
    }

    public void setOnPaymentListener(OnPaymentListener paymentListener) {
        this.paymentListener = paymentListener;
    }

    @Override
    public int getCount() {
        return EmptyUtil.isEmpty(data) ? 0 : data.size();
    }

    @Override
    public OrderInfo getItem(int position) {
        return EmptyUtil.isEmpty(data) ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.item_order_list, parent, false);
            holder.logo = (RoundRectImageView) view.findViewById(R.id.logo_img);
            holder.name = (TextView) view.findViewById(R.id.name_txt);
            holder.date = (TextView) view.findViewById(R.id.date_txt);
            holder.price = (TextView) view.findViewById(R.id.price_txt);
            holder.actionBtn = (Button) view.findViewById(R.id.action_btn);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        final OrderInfo item = getItem(position);
        Thumb.getInstance().display(holder.logo, item.getLogo(), R.drawable.def_logo);
        holder.name.setText(item.getName());
        holder.date.setText(DateUtil.formatDate(item.getCtime(), "yyyy-MM-dd"));
        holder.price.setText("￥" + item.getTotalPrice());

        if ("P".equals(item.getType())){
            holder.actionBtn.setText("待付款");
            holder.actionBtn.setEnabled(true);
            holder.actionBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (paymentListener != null) {
                        paymentListener.onPayment(item);
                    }
                }
            });
        } else {
            switch (item.getType()) {
                case "C":
                    holder.actionBtn.setText("已完成");
                    break;
                case "N":
                    holder.actionBtn.setText("已取消");
                    break;
                case "Q":
                    holder.actionBtn.setText("付现待确认");
                    break;
            }
            holder.actionBtn.setEnabled(false);
            holder.actionBtn.setOnClickListener(null);
        }
        return view;
    }

    private class ViewHolder{
        private RoundRectImageView logo;
        private TextView name;
        private TextView date;
        private TextView price;
        private Button actionBtn;
    }

    public interface OnPaymentListener {

        void onPayment(OrderInfo order);
    }
}
